package com.ipzoe.web.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ipzoe.cms.domain.Agreement;
import com.ipzoe.cms.service.IAgreementService;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.core.page.TableDataInfo;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 协议Controller
 * 
 * @author eric
 * @date 2020-08-15
 */
@RestController
@RequestMapping("/cms/agreement" )
public class AgreementController extends BaseController {

    @Autowired
    private IAgreementService iAgreementService;

    /**
     * 查询协议列表
     */
    @PreAuthorize("@ss.hasPermi('cms:agreement:list')")
    @GetMapping("/list")
    public TableDataInfo list(Agreement agreement)
    {
        startPage();
        LambdaQueryWrapper<Agreement> lqw = new LambdaQueryWrapper<Agreement>();
        lqw.like(StringUtils.isNotBlank(agreement.getName()), Agreement::getName ,agreement.getName());
        lqw.eq(agreement.getPackageId() != null, Agreement::getPackageId, agreement.getPackageId());
        List<Agreement> list = iAgreementService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 获取协议详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:agreement:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iAgreementService.getById(id));
    }

    /**
     * 新增协议
     */
    @PreAuthorize("@ss.hasPermi('cms:agreement:add')" )
    @Log(title = "协议" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Agreement agreement) {

        agreement.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iAgreementService.save(agreement) ? 1 : 0);
    }

    /**
     * 修改协议
     */
    @PreAuthorize("@ss.hasPermi('cms:agreement:edit')" )
    @Log(title = "协议" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agreement agreement) {

        agreement.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iAgreementService.updateById(agreement) ? 1 : 0);
    }

    /**
     * 删除协议
     */
    @PreAuthorize("@ss.hasPermi('cms:agreement:remove')" )
    @Log(title = "协议" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iAgreementService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
